package org.jeecg.compare;



import com.google.gson.Gson;
import org.jeecg.compare.enums.ChangeTypeEnum;
import org.jeecg.compare.enums.ExceptionEnum;
import org.jeecg.compare.exception.CompareException;
import org.jeecg.compare.model.ChangeModel;
import org.jeecg.compare.pojo.User;
import org.jeecg.compare.util.CompareHelper;

import java.util.*;

/**
 * 功能描述: 比较核心方法 <br/>
 */
public class CompareCore {

    /**
     * 功能描述: 列表比较 <br/>
     *
     * @param before 之前
     * @param after 之后
     */
    public static <E> List<ChangeModel> compare(Collection<E> before, Collection<E> after) throws IllegalAccessException, InstantiationException {
        List<ChangeModel> changeList = new ArrayList<>();
        CompareHelper.compareProcess(changeList, before, after);
        return changeList;
    }

    /**
     * 功能描述: 对象比较 <br/>
     *
     * @param before 之前
     * @param after 之后
     * @return "java.util.List<com.cah.project.compare.model.ChangeModel>"
     */
    public static List<ChangeModel> compare(Object before, Object after) throws IllegalAccessException, InstantiationException {
        // 校验 o1 和 o2 的类型是一致的
        if(!before.getClass().equals(after.getClass())) {
            throw new CompareException(ExceptionEnum.INCONSISTENT_CLASS);
        }
        return compare(Collections.singletonList(before), Collections.singletonList(after));
    }
    private static List<User> getUserListLeft() {
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setIdCard("11111");
        user.setName("张三");
        list.add(user);


        User user1 = new User();
        user1.setIdCard("11112");
        user1.setName("李四");

        User user11 = new User();
        user11.setIdCard("1122");
        user11.setName("利佩欧");
        user1.setSpouse(user11);

        List<User> children = new ArrayList<>();
        User user111 = new User();
        user111.setIdCard("11221");
        user111.setName("利斯海1");
        children.add(user111);
        user1.setChildren(children);

        User user112 = new User();
        user112.setIdCard("11222");
        user112.setName("利斯海2");
        children.add(user112);
        user1.setChildren(children);
        list.add(user1);

        return list;
    }

    private static List<User> getUserListRight() {
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setIdCard("11111");
        user.setName("张三");
        list.add(user);


        User user1 = new User();
        user1.setIdCard("11112");
        user1.setName("李四");

        User user11 = new User();
        user11.setIdCard("1122");
        user11.setName("利佩欧");
        user1.setSpouse(user11);

        List<User> children = new ArrayList<>();
        User user111 = new User();
        user111.setIdCard("11221");
        user111.setName("利斯海1");
        children.add(user111);
        user1.setChildren(children);

        User user112 = new User();
        user112.setIdCard("11222");
        user112.setName("利斯海22");
        children.add(user112);
        user1.setChildren(children);
        list.add(user1);

        return list;
    }
    public static void extractModifiedChanges(List<ChangeModel> changeModels) {
        for (ChangeModel changeModel : changeModels) {
            if (changeModel.getChangeType() == ChangeTypeEnum.MODIFIED) {
                System.out.println("变化类型：" + changeModel.getChangeType());
                System.out.println("类型名：" + changeModel.getTypeName());
                compareAndPrintChanges(changeModel.getBefore(), changeModel.getAfter());
            }
            if (changeModel.getChildren() != null) {
                extractModifiedChanges(changeModel.getChildren());
            }
        }
    }

    public static void compareAndPrintChanges(Object before, Object after) {
        // 在这里实现比较并打印变化信息的逻辑
        // 你可以根据你的数据结构，例如比较每个字段的变化
        // 这里简单示范一下如何打印 before 和 after 的值
        System.out.println("改变前：" + before);
        System.out.println("改变后：" + after);
        System.out.println("----------------------");
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        List<ChangeModel> compare = CompareCore.compare(getUserListLeft().get(1), getUserListRight().get(1));
        extractModifiedChanges(compare);

    }
    
}
